package com.gy.niuer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gy.niuer.domain.Appointment;
import com.gy.niuer.domain.Patient;
import com.gy.niuer.mapper.AppointmentMapper;
import com.gy.niuer.service.PatientService;
import com.gy.niuer.mapper.PatientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
*
*/
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient>
implements PatientService{

    @Autowired
    PatientMapper patientMapper;

    @Autowired
    AppointmentMapper appointmentMapper;

    @Override
    public boolean insertPatient(Patient patient) {
        System.out.println(patient);
        int insert = patientMapper.insert(patient);
        if (insert != 0)
            return true;
        return false;
    }

    @Override
    public List<Patient> selectAllPatient() {
        return patientMapper.selectAll();
    }

    @Override
    public void addPatient(Patient patient) {
        patientMapper.addPatient(patient);
    }

    @Override
    public boolean delPatient(int id) {
        //就诊人的挂号信息
//        QueryWrapper<Appointment> wrapper = new QueryWrapper<>();
//        wrapper.eq("patient", id);
//        Appointment appointment = appointmentMapper.selectOne(wrapper);
        Patient patient = patientMapper.selectOneById(id);
        Appointment appointment = appointmentMapper.selectOneByPatient(id);
        if(patient != null){
            if(appointment != null){
                int delete = appointmentMapper.deleteByPatient(id);
                int i = patientMapper.deleteById(id);
                if(delete != 0 && i != 0)
                    return true;
                else
                    return false;
            }else{
                int i = patientMapper.deleteById(id);
                if(i != 0)
                    return true;
                else
                    return false;
            }

        }else
            return false;
    }
}
